package com.example.txjc.Activity;

import java.io.File;      
import java.io.FileOutputStream;      
import java.io.IOException;

import android.app.Activity;
import android.content.pm.ActivityInfo;      
import android.graphics.Bitmap;      
import android.graphics.BitmapFactory;      
import android.graphics.Matrix;
import android.graphics.PixelFormat;      
import android.hardware.Camera;      
import android.hardware.Camera.AutoFocusCallback;
import android.hardware.Camera.PictureCallback;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;      
import android.view.View;      
import android.view.Window;      
import android.view.SurfaceHolder.Callback;      
import android.view.View.OnClickListener;      
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.example.txjc.R;


public class CameraTest extends Activity implements
Callback, OnClickListener, AutoFocusCallback{      
    SurfaceView mySurfaceView;//surfaceView����      
    SurfaceHolder holder;//surfaceHolder����      
    Camera myCamera;//�������      
    String filePath="/sdcard/JSS.jpg";//��Ƭ����·��      
    boolean isClicked = false;//�Ƿ�����ʶ      
    //����jpegͼƬ�ص����ݶ���      
    PictureCallback jpeg = new PictureCallback() {      
              
        @Override      
        public void onPictureTaken(byte[] data, Camera camera) {      
            // TODO Auto-generated method stub      

        	 Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);  
//        	 int width = bitmap.getWidth();
//        	 int height = bitmap.getHeight();
//        	 System.out.println("!!!!!!!!!!!!!!"+width);
//        	 System.out.println("!!!!!!!!!!!!!!"+height);
        	 
        	 Matrix matrix = new Matrix();
        	 matrix.preRotate(90);  
        	 bitmap = Bitmap.createBitmap(bitmap ,0,0, 2400, 1500,matrix,true);
             File file = new File(filePath);      
            
        	try      
            {
            	// ���ͼƬ      
	            FileOutputStream bos = new FileOutputStream(file);      
	            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos);
	            bos.flush();//���      
	            bos.close();//�ر�      
	        }
            catch(Exception e)      
            {      
                e.printStackTrace();      
            }      
                  
        }      
    };      
    /** Called when the activity is first created. */      
    @Override      
    public void onCreate(Bundle savedInstanceState) {      
        super.onCreate(savedInstanceState);      
        requestWindowFeature(Window.FEATURE_NO_TITLE);//�ޱ���                 
        //�������㷽��      
        this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);      
        //���ó�ȫ��ģʽ
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
        		WindowManager.LayoutParams.FLAG_FULLSCREEN);
        
        setContentView(R.layout.main);
        Toast.makeText(CameraTest.this,"拍照成功",Toast.LENGTH_SHORT);
        LinearLayout main = (LinearLayout)findViewById(R.id.main);
        LinearLayout lay = (LinearLayout)findViewById(R.id.lay);
        lay.setVisibility(LinearLayout.INVISIBLE);
        
        //��ÿؼ�      
        mySurfaceView = (SurfaceView)findViewById(R.id.surfaceView1);      
        //��þ��      
        holder = mySurfaceView.getHolder();      
        //��ӻص�      
        holder.addCallback(this);      
        //��������      
        holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);      
        //���ü���      
        main.setOnClickListener(this);      
    }      
    @Override      
    public void surfaceChanged(SurfaceHolder holder, int format, int width,      
            int height) {      
        // TODO Auto-generated method stub      
        //���ò�������ʼԤ��      
        Camera.Parameters params = myCamera.getParameters();      
        params.setPictureFormat(PixelFormat.JPEG);      
        params.setPreviewSize(640,480); 
        myCamera.setParameters(params);      
        myCamera.startPreview();      
              
    }      
    @Override      
    public void surfaceCreated(SurfaceHolder holder) {      
        // TODO Auto-generated method stub      
        //�������      
        if(myCamera == null)      
        {      
            myCamera = Camera.open();      
            try {      
                myCamera.setPreviewDisplay(holder);      
            } catch (IOException e) {      
                // TODO Auto-generated catch block      
                e.printStackTrace();      
            }      
        }      
              
    }      
    @Override      
    public void surfaceDestroyed(SurfaceHolder holder) {      
        // TODO Auto-generated method stub      
        //�ر�Ԥ�����ͷ���Դ      
        myCamera.stopPreview();      
        myCamera.release();      
        myCamera = null;      
              
    }      
    @Override      
    public void onClick(View v) {      
        // TODO Auto-generated method stub      
        if(!isClicked)      
        {      
            myCamera.autoFocus(this);//�Զ��Խ�      
            isClicked = true;      
        }else      
        {      
            myCamera.startPreview();//����Ԥ��      
            isClicked = false;      
        }      
              
    }      
    @Override      
    public void onAutoFocus(boolean success, Camera camera) {

        // TODO Auto-generated method stub      
        if(success)      
        {      
            //���ò���,������      
            Camera.Parameters params = myCamera.getParameters();      
            params.setPictureFormat(PixelFormat.JPEG);      
            params.setPreviewSize(640,480); 
            myCamera.setParameters(params);      
            myCamera.takePicture(null, null, jpeg);

        }      
    }   
}      